package com.gc.monitor.gctask.impl;

import com.gc.gpsoo.GpsooSDK;
import com.gc.gpsoo.beans.GpsooCarData;
import com.gc.gpsoo.beans.GpsooDevData;
import com.gc.gpsoo.exception.GpsooSDKException;
import com.gc.common.entity.Alert;
import com.gc.monitor.gcexception.GCMonitorException;
import com.gc.monitor.gctask.GpsooMonitor;
import com.gc.monitor.gctask.GpsooMonitorResult;
import com.gc.monitor.gctask.MonitorStatusPojo;
import com.gc.frontend.service.AlertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * 分析并生成报警
 * Created by 瑱彬 on 2015/4/23.
 */
@Component(value = "gpsooAnalysisAlert")
@Scope("singleton")
public class GpsooAnalysisAlertImpl implements GpsooMonitor {
    private Map<String, GpsooDevData> gpsooDevDataMap;
    private Map<String, GpsooCarData> gpsooCarDataMap;
    private Map<String, MonitorStatusPojo> monitorStatusPojoMap;

    @Autowired
    private AlertService alertService;

    @Override
    public void run() throws GCMonitorException {
        gpsooCarDataMap = GpsooMonitorResult.getInstance().getMonitorMap();
        gpsooDevDataMap = GpsooMonitorResult.getInstance().getDevinfoMap();
        monitorStatusPojoMap = GpsooMonitorResult.getInstance().getLatestHistoriesStatus();
        //遍历每一辆车
        for (GpsooCarData gpsooCarData : gpsooCarDataMap.values()) {
            checkMoveAlert(gpsooCarData);
            checkOverspeedAlert(gpsooCarData);
            checkPowerAlert(gpsooCarData);
            checkVibrateAlert(gpsooCarData);
        }
    }

    //断电警告
    public void checkPowerAlert(GpsooCarData gpsooCarData) {
        int status = gpsooCarData.getDeviceInfo();
        if (GpsooCarData.OFFLINE.equals(status)) {
            insertAlert(Alert.POWER_OFF, gpsooCarData);
        }
    }

    //振动警告
    public void checkVibrateAlert(GpsooCarData gpsooCarData) {
        //暂时无法判断
    }

    /**
     * 位移警告，不能再停止的同时还改地址！
     */
    public void checkMoveAlert(GpsooCarData gpsooCarData) {
        MonitorStatusPojo monitorStatusPojo = monitorStatusPojoMap.get(gpsooCarData.getImei());
        boolean posChangAndStop = monitorStatusPojo.isStopStatus() && monitorStatusPojo.isPosChangeStatus();
        int nowTimestamp = new Long(new Date().getTime() / 1000).intValue();
        if (nowTimestamp - monitorStatusPojo.getTime() > 60 && posChangAndStop) {
            insertAlert(Alert.MOVE, gpsooCarData);
        }

    }

    /**
     * 超速警告，不能超速超到60以上
     */
    public void checkOverspeedAlert(GpsooCarData gpsooCarData) {
        if (gpsooCarData.getSpeed() > 60) {
            insertAlert(Alert.OVERSPEED, gpsooCarData);
        }
    }


    public void insertAlert(Byte type, GpsooCarData gpsooCarData) {
        Alert alert = new Alert();
        alert.setAlertIsprocessed(Alert.UNPROCESS);
        alert.setAlertPoslat(gpsooCarData.getLat() + "");
        alert.setAlertPoslong(gpsooCarData.getLng() + "");
        try {
            alert.setAlertPosname(GpsooSDK.getInstance().loadAddress(alert.getAlertPoslong(), alert.getAlertPoslat()));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GpsooSDKException e) {
            e.printStackTrace();
        }
        alert.setAlertTime(new Date());
        alert.setAlertType(type);

        alertService.insertSelective(alert);
    }
}
